package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.ReportRhlqCcwdxBaDetail;
import com.ruoyi.report_lq.domain.vo.ReportRhlqCcwdxBaDetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportRhlqPrsdsyBaDetailBo;
import com.ruoyi.report_lq.domain.vo.ReportRhlqPrsdsyBaDetailVo;
import com.ruoyi.report_lq.domain.ReportRhlqPrsdsyBaDetail;
import com.ruoyi.report_lq.mapper.ReportRhlqPrsdsyBaDetailMapper;
import com.ruoyi.report_lq.service.IReportRhlqPrsdsyBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 乳化沥青破乳速度记录Service业务层处理
 *
 * @author zyb
 * @date 2024-06-28
 */
@RequiredArgsConstructor
@Service
public class ReportRhlqPrsdsyBaDetailServiceImpl implements IReportRhlqPrsdsyBaDetailService {

    private final ReportRhlqPrsdsyBaDetailMapper baseMapper;

    /**
     * 查询乳化沥青破乳速度记录
     */
    @Override
    public ReportRhlqPrsdsyBaDetailVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     *
     * @param infoId
     * @return 破乳速度集合
     * @method 根据info-id 查询破乳速度子表的list集合数据
     */
    public List<ReportRhlqPrsdsyBaDetailVo> queryByInfoId (Long infoId) {
        QueryWrapper<ReportRhlqPrsdsyBaDetail> query = new QueryWrapper<>();
        query.eq("common_id", infoId);
        return baseMapper.selectVoList(query);
    }

    public ReportRhlqPrsdsyBaDetailVo queryVoByInfoId (Long infoId) {
        QueryWrapper<ReportRhlqPrsdsyBaDetail> query = new QueryWrapper<>();
        query.eq("common_id", infoId);
        ReportRhlqPrsdsyBaDetailVo reVo = baseMapper.selectVoOne(query);
        if(BeanUtil.isEmpty(reVo)) {
            return new ReportRhlqPrsdsyBaDetailVo();
        }
        return baseMapper.selectVoOne(query);
    }


    /**
     * 查询乳化沥青破乳速度记录列表
     */
    @Override
    public TableDataInfo<ReportRhlqPrsdsyBaDetailVo> queryPageList(ReportRhlqPrsdsyBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportRhlqPrsdsyBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportRhlqPrsdsyBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询乳化沥青破乳速度记录列表
     */
    @Override
    public List<ReportRhlqPrsdsyBaDetailVo> queryList(ReportRhlqPrsdsyBaDetailBo bo) {
        LambdaQueryWrapper<ReportRhlqPrsdsyBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportRhlqPrsdsyBaDetail> buildQueryWrapper(ReportRhlqPrsdsyBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportRhlqPrsdsyBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommonId() != null, ReportRhlqPrsdsyBaDetail::getCommonId, bo.getCommonId());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglzlA()), ReportRhlqPrsdsyBaDetail::getKuanglzlA, bo.getKuanglzlA());
        lqw.eq(StringUtils.isNotBlank(bo.getWaijslA()), ReportRhlqPrsdsyBaDetail::getWaijslA, bo.getWaijslA());
        lqw.eq(StringUtils.isNotBlank(bo.getRuyylA()), ReportRhlqPrsdsyBaDetail::getRuyylA, bo.getRuyylA());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglbhjgmsA()), ReportRhlqPrsdsyBaDetail::getKuanglbhjgmsA, bo.getKuanglbhjgmsA());
        lqw.eq(StringUtils.isNotBlank(bo.getPorsdA()), ReportRhlqPrsdsyBaDetail::getPorsdA, bo.getPorsdA());
        lqw.eq(StringUtils.isNotBlank(bo.getPorsdfjhdh()), ReportRhlqPrsdsyBaDetail::getPorsdfjhdh, bo.getPorsdfjhdh());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglzlB()), ReportRhlqPrsdsyBaDetail::getKuanglzlB, bo.getKuanglzlB());
        lqw.eq(StringUtils.isNotBlank(bo.getWaijslB()), ReportRhlqPrsdsyBaDetail::getWaijslB, bo.getWaijslB());
        lqw.eq(StringUtils.isNotBlank(bo.getRuyylB()), ReportRhlqPrsdsyBaDetail::getRuyylB, bo.getRuyylB());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglbhjgmsB()), ReportRhlqPrsdsyBaDetail::getKuanglbhjgmsB, bo.getKuanglbhjgmsB());
        lqw.eq(StringUtils.isNotBlank(bo.getPorsdB()), ReportRhlqPrsdsyBaDetail::getPorsdB, bo.getPorsdB());
        return lqw;
    }

    /**
     * 新增乳化沥青破乳速度记录
     */
    @Override
    public Boolean insertByBo(ReportRhlqPrsdsyBaDetailBo bo) {
        ReportRhlqPrsdsyBaDetail add = BeanUtil.toBean(bo, ReportRhlqPrsdsyBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改乳化沥青破乳速度记录
     */
    @Override
    public Boolean updateByBo(ReportRhlqPrsdsyBaDetailBo bo) {
        ReportRhlqPrsdsyBaDetail update = BeanUtil.toBean(bo, ReportRhlqPrsdsyBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 修改乳化沥青破乳速度记录
     */
    public void insertOrUpdate(ReportRhlqPrsdsyBaDetailBo bo, Long pId) {
        ReportRhlqPrsdsyBaDetail detail = BeanUtil.toBean(bo, ReportRhlqPrsdsyBaDetail.class);
        detail.setCommonId(pId);
        baseMapper.insertOrUpdate(detail);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportRhlqPrsdsyBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除乳化沥青破乳速度记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
